home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / c1.arc / C1.TXT next >
Text File  |  1984-12-26  |  5KB  |  137 lines

  1. Copyright (c) 1984 by Daniel L. Roady.    All rights reserved.
  2. ╔═════════════════════════════════════════════════════════════════════════════╗
  3. ║              THE "STANDARD I/O LIBRARY"                          ║
  4. ║                                          ║
  5. ║                    by                          ║
  6. ║                                          ║
  7. ║                  Daniel L. Roady                      ║
  8. ╚═════════════════════════════════════════════════════════════════════════════╝
  9.  
  10.  
  11. ┌──────────────────────────────────┐
  12. │ Introduction               │
  13. └──────────────────────────────────┘
  14.  
  15.            This  is the first in a series of articles describing a
  16.       "UNIX"   compatible  implementation  of  the  "Standard  I/O
  17.       Library."  The  library  contains  the members listed in the
  18.       following  table  and  several standard macros for character
  19.       handling.
  20.  
  21.  
  22.           ┌─────────────────────┬────────────────────┐
  23.           │      getchar    │      putchar         │
  24.           ├─────────────────────┼────────────────────┤
  25.           │      printf    │      scanf         │
  26.           ├─────────────────────┼────────────────────┤
  27.           │      fopen     │      fclose         │
  28.           ├─────────────────────┼────────────────────┤
  29.           │      getc        │      putc         │
  30.           ├─────────────────────┼────────────────────┤
  31.           │      ungetc    │      calloc         │
  32.           ├─────────────────────┼────────────────────┤
  33.           │      cfree     │      strlen         │
  34.           ├─────────────────────┼────────────────────┤
  35.           │      strcpy    │      strcat         │
  36.           ├─────────────────────┼────────────────────┤
  37.           │      strcmp    │             │
  38.           └─────────────────────┴────────────────────┘
  39.  
  40.  
  41.            This  article discusses the printf function. The source
  42.       code    for  this  function can be  viewed by pressing the Esc
  43.       key, followed by the F2 key.
  44.  
  45. ┌──────────────────────────────────┐
  46. │ Printf Overview           │
  47. └──────────────────────────────────┘
  48.  
  49.            Printf    provides   automatic   formatting   and   type
  50.       conversions  for  output  to    the  standard  output  channel
  51.       ("stdout").
  52.  
  53.  
  54.            The printf calling sequence is:
  55.  
  56.  
  57.             printf(control-string,  arg1,  arg2  ....);
  58.  
  59.  
  60.            The  parameter "control-string" is actually a character
  61.       pointer to a quoted string containing the text to be output.
  62.       "Control-string"  may  additionally  contain  formatting and
  63.       type conversion specifications.
  64.  
  65.            The  arguments  ( arg1, arg2, etc ) correspond with the
  66.       conversion specifications in "control-string."
  67.  
  68.            A fuller discussion of the printf function can be found
  69.       in "The C Programming Language" by Kernighan and Ritchie.
  70.  
  71. ┌──────────────────────────────────┐
  72. │ Printf Source Code package       │
  73. └──────────────────────────────────┘
  74.  
  75.            The   printf   package  consists  of  three  externally
  76.       accessible  routines:  printf,  fprintf,  and  sprintf. Each
  77.       routine performs the same formatting and conversion function
  78.       but the output destinations are different. Printf outputs to
  79.       stdout;  fprintf  outputs  to  a specified file; and sprintf
  80.       outputs to a memory buffer.
  81.  
  82.            A typical call to each routine is shown below.
  83.  
  84.  
  85.         ┌──────────────────────────────────────────────────────┐
  86.         │    #include    <stdio.h>               │
  87.         │                               │
  88.         │        FILE    *fopen(),*fp;               │
  89.         │        char    buffer[133];               │
  90.         │                               │
  91.         │    main()                           │
  92.         │        {                       │
  93.         │        printf("It's a small world.\n");           │
  94.         │                               │
  95.         │        fp = fopen("outfile.txt","w");             │
  96.         │        fprintf(fp,"It's a small world.\n");       │
  97.         │        fclose(fp);                   │
  98.         │                               │
  99.         │        sprintf(buffer,"It's a small world.\n");   │
  100.         │        }                       │
  101.         └──────────────────────────────────────────────────────┘
  102.  
  103.  
  104.            The  printf package is written as three entry points to
  105.       handle the details of the output selection. Each entry point
  106.       performs  some  specific setup on a few global variables and
  107.       calls the routine "outf."
  108.  
  109.            "Outf"  scans  the  control-string, searching for a "%"
  110.       character.  If the character read is not a "%", it is passed
  111.       to  the output routine "putchar." If the character is a "%",
  112.       the  succeeding characters are examined and global variables
  113.       are initialized to indicate left or right justification, the
  114.       pad character, field width, precision, long or short, or the
  115.       conversion type. Once the conversion type is determined, the
  116.       appropriate parameter is passed to the routine that performs
  117.       the particular conversion.
  118.  
  119.            The conversion routines create ASCII strings from their
  120.       input parameters. The strings are output, temporarily, to an
  121.       internal   buffer.   The  routine  "obstore"  performs  this
  122.       function.  When conversion is complete, the routine "bfrout"
  123.       inserts   any   necessary   padding    for   left   or  right
  124.       justification  and  outputs  the  entire converted field via
  125.       "putchar."
  126.  
  127.            "Outf"  continues  to scan the control-string until the
  128.       entire control-string has been searched.
  129.  
  130.  
  131.  
  132.  
  133.  
  134.              ┌──────────────────────────────────┐
  135.              │ File Name: ██     c1.txt     ██    │
  136.              └──────────────────────────────────┘
  137.